﻿using System.Web.SessionState;

namespace App.Utility.StateContainers
{
    public class SessionStateContainer<T>
    {
        HttpSessionState SessionState;
        string m_key;
        public SessionStateContainer(string Key, HttpSessionState bag)
        {
            m_key = Key;
            SessionState = bag;
            //cost:0.0397s
            //HttpContext.Current.Trace.Warn("Key '" + Key + "' is " + ((bag[Key] == null) ? "NOT FOUND" : "found"));
        }

        public SessionStateContainer(string Key, HttpSessionState bag, T defaultValue)
        {
            m_key = Key;
            SessionState = bag;
            if (SessionState[m_key] == null)
                Value = defaultValue;
        }

        public T Value
        {
            get
            {
                //cost:0.0765s
                //HttpContext.Current.Trace.Warn("Key '" + m_key + "' is " + ((AppState[m_key] == null) ? "NOT FOUND" : "found"));
                if (SessionState[m_key] == null)
                    return default(T);
                return (T)SessionState[m_key];
            }
            set
            {
                //cost:0.0001s
                //HttpContext.Current.Trace.Warn("Setting Key '" + m_key + "'");
                SessionState[m_key] = value;
            }
        }
    }
}
